﻿/* 
* Copyright (c) 2009, SCUT 06级计科2班软件工程第6小组
* All rights reserved. 
*
* 作    者：
* 完成日期：
*  
* 摘    要：档案类以及档案评论类
*
* 备    注：
*
*/

using System;
using System.Web.SessionState;
using System.Collections.Generic;
using System.Web;

public enum ArchiveSecurityLevel{
	Public=0,//完全公开
	Share = 1,//注册用户可访问
	Private=2,//档案拥有者可访问
	Safe=3,//加密级别，拥有者可访问（档案已加密）
}
public enum ArchiveType{
	Video=1 <<0,
	Audio=1 <<1,
	Text=1 <<2,
	Image=1 <<3,
	Rar=1<<4,
	Unknow = 1 << 5,
	All = ((1<<6) - 1),
}
public enum ArchiveState{
    Normal = 0,
    Hidden = 1
}

public class ArchiveCategory{
	public String Name;
	public Int32 Count;
    public ArchiveCategory(String name,Int32 count)
    {
        Name = name;
        Count = count;
    }
}
public class Archive
{
	public const Int32 NON_EXISTS_ID=0;
	//针对档案上传
    public Archive(
		Int32 ownerID, 
		String name, String[] keywords, Int32 point, String description, 
		Byte[] content,ArchiveType type,	String category,ArchiveSecurityLevel level,Int32 size)
    {
        _ownerID = ownerID;
		_name = name;
        _keywords = keywords;
        _point = point;
        _description = description;		
		_content = content;
		_type = type;
		if (!Utility.Util.IsEmptyString(category))
			_category = category;
		else
			_category = "默认分类";
		_securityLevel = level;
		//初始值
        _createTime = System.DateTime.Now.ToString(); //获取当前时间
        _viewCount = 0;
        _downloadCount = 0;
		_size = size;
    }
    //针对数据库查询
    public Archive(
		Int32 id, Int32 ownerID, String name, String[] keywords, Int32 point, String description,
		ArchiveType type, ArchiveSecurityLevel securityLevel, String category,
		String createTime, Int32 viewCount, Int32 downloadCount,Int32 size,
		Byte[] content,ArchiveState state )
    {
        _id = id;
        _name = name;
        _ownerID = ownerID;
        _keywords = keywords;
        _point = point;
        _description = description;
        _category = category;
        _securityLevel = securityLevel;
        _createTime = createTime;
        _viewCount = viewCount;
        _downloadCount = downloadCount;
		_type=type;
        _size = size;
        _content = content;
        _state = state;
    }
	public void Update(Int32 point,String description,String category)
	{
		if (point > 0)
			_point = point;
		if (!Utility.Util.IsEmptyString(description))
			_description = description;
		if (!Utility.Util.IsEmptyString(category))
			_category = category.Trim();
	}
	//针对档案下载时，从数据库创建
	public Archive(Byte[] content,String name,Int32 point)
	{
		_content = content;
		_name = name;
		_point = point;
	}
	public Boolean IsHidden()
	{
		return _state != ArchiveState.Normal;
	}
	//用XHTML标签格式化输出档案的缩略描述
	public String SimpleRender()
	{
		return 
			"<div class='srItem'>"+
				"<a class='name' href='ViewDetailArchive.aspx?id=" + _id + "' target='_blank'>" + 
				GetLevelDescription()+	_name+"("+ReadableSize(_size)+")</a>"+
				GetOpURL()+ "<br />" +
				"<div class='desp'>"+_description+"</div>"+
				"<div class='cntStat'>被下载<span class='cue'>" + _downloadCount + 
					"</span>次，被浏览<span class='cue'>" + _viewCount + "</span>次</div>" +
				"相关关键词：" +
				((_keywords[0]=="") ? "":("<span>"+_keywords[0]+"</span>"))+
				((_keywords[1] == "") ? "" : ("<span>" + _keywords[1] + "</span>")) +
				((_keywords[2] == "") ? "" : ("<span>" + _keywords[2] + "</span>")) +
				((_keywords[3] == "") ? "" : ("<span>" + _keywords[3] + "</span>")) +
				((_keywords[4] == "") ? "" : ("<span>" + _keywords[4] + "</span>")) +
			"</div>";
	}
	//用XHTML标签格式化档案的详细描述
	public String Render()
	{
		SE6.User logUsr=SessionMan.GetLoginUser();
		Boolean canUpdate =(logUsr != null) && logUsr.CanUpdate(this);
		return
				"<table>" +
						"<tr><td class='w'>" + GetLevelDescription()+
							" 档案名称:</td><td>" + _name + "</td></tr>" +
						"<tr><td>档案拥有者:</td><td>" +SE6.UserMan.GetUserProfileURL(_ownerID)
								+ "</td></tr>" +
						"<tr><td>关键字(Tag):</td><td>"+
							((_keywords[0] == "") ? "" : ("<span>" + _keywords[0] + "</span>")) +
							((_keywords[1] == "") ? "" : ("<span>" + _keywords[1] + "</span>")) +
							((_keywords[2] == "") ? "" : ("<span>" + _keywords[2] + "</span>")) +
							((_keywords[3] == "") ? "" : ("<span>" + _keywords[3] + "</span>")) +
							((_keywords[4] == "") ? "" : ("<span>" + _keywords[4] + "</span>")) +
						" </td></tr>" +
						"<tr><td>档案价值分:</td><td>" +GUI.SmartText(canUpdate,"point",_point)+"</td></tr>" +
						"<tr><td>档案描述:</td><td>" +GUI.SmartText(canUpdate,"dpt",_description)+"</td></tr>" +
						"<tr><td>分类目录:</td><td>" + GUI.SmartText(canUpdate,"cat",_category) + "</td></tr>" +
						"<tr><td>立档时间:</td><td class='time'>" + _createTime+"</td></tr>" +
						"<tr><td>浏览次数:</td><td>" + _viewCount + "</td></tr>" +
						"<tr><td>下载次数:</td><td>" + _downloadCount + "</td></tr>" +
						"<tr><td>档案操作:</td><td>"+ GetOpURL() +
						"</td></tr>" +
					"</table>"+GUI.SmartSubmit(canUpdate,_id);
	}
    public Int32 ID
    {
        get { return _id; }              //编号
        set { _id= value; }              //编号
    }
    public String Name
    {
        get { return _name; }           //档案名
    }
    public Int32 OwnerID
    {
        get { return _ownerID; }         //拥有者标识
    }
    public String[] Keywords
    {
        get { return _keywords; }       //关键字
    }
    public Int32 Point
    {
        get { return _point; }           //档案分
    }
    public String Description
    {
        get { return _description; }    //档案描述
    }
    public String Category
    {
        get { return _category; }       //档案分类
    }
    public Byte[] Content
    {
        get { return _content; }        //档案内容
    }
    public ArchiveSecurityLevel SecurityLevel
    {
        get { return _securityLevel; }   //档案级别
    }
    public String CreateTime
    {
        get { return _createTime; }     //立档时间
    }
    public Int32 ViewCount
    {
        get { return _viewCount; }       //档案被浏览的次数
    }
	public Int32 DownloadCount
    {
        get { return _downloadCount; }  //档案被下载的次数
    }

	public ArchiveType Type
    {
        get { return _type; }
    }
	public Int32 Size
	{
		get{return _size;}
	}

    public ArchiveState State
    {
        get { return _state; }
    }

	private static String ReadableSize(Int32 byteCount)
	{
		String[] unit ={" B"," KB"," MB"," GB"," TB"};
		Double bc = byteCount;
		for(Int32 i=0;i<unit.Length;++i)
		{
			if (bc < 1024)
				return bc.ToString("F3")+unit[i];
			bc /= 1024;
		}
		return "文件大小出现异常";
	}
	private String GetOpURL()
	{
		SE6.User logUsr=SessionMan.GetLoginUser();
		return GetDownloadURL() + (logUsr != null && logUsr.CanDelete(this) ? GetDeleteURL() : "");
	}
	private String GetDownloadURL()
	{
		return "<a class='button' href='par.aspx?t=2&id=" + _id +"' target='_blank'>下载("+
			(_securityLevel==ArchiveSecurityLevel.Public?"免费":("需扣积分"+_point))
			+")"+"</a>";
	}
	private String GetDeleteURL()
	{
		return "<a class='button' href='par.aspx?t=5&id=" + _id + "' target='_blank'>删除(请慎重)</a>";
	}
	private String GetLevelDescription()
	{
		String[] desc = { "[公开]", "[共享]", "[私有]", "[安全]" };
		return desc[(Int32)_securityLevel];
	}

    private Int32 _id;              //编号
    private String _name;           //档案名
    private Int32 _ownerID;         //拥有者标识
    private String[] _keywords;       //关键字组（5个） 
    private Int32 _point;           //档案分
    private String _description;    //档案描述
    private String _category;       //档案分类
    private Byte[] _content;        //档案内容
    private ArchiveSecurityLevel _securityLevel;  //档案级别
    private String _createTime;     //立档时间
    private Int32 _viewCount;       //档案被浏览的次数
    private Int32 _downloadCount;   //档案被下载的次数
    private ArchiveType _type;
	private Int32 _size;//档案字节数
    private ArchiveState _state;           //档案状态
}
public class ArchiveComment
{
	private Int32 _id;//评论编号
	private Int32 _archiveID;//被评论的档案ID
	private Int32 _userID;//评论档案的用户ID
	private String _content;//评论内容

    //如何确定 (评论编号)_id ?
    public ArchiveComment( Int32 Id , Int32 ArchiveID, Int32 UserID, String Content)
    {
        _id = Id; 
        _archiveID = ArchiveID ;
        _userID = UserID ;
        _content = Content; 
    }
}